package com.example.online_retailers.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.online_retailers.dto.Result;
import com.example.online_retailers.entity.TradeInfo;
import com.example.online_retailers.entity.Wallet;
import com.example.online_retailers.service.TradeInfoService;
import com.example.online_retailers.service.WalletService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
@Slf4j
@RestController
@RequestMapping("/wallet")
public class WalletController {
    @Resource
    private WalletService WalletService;
    @Resource
    private TradeInfoService tradeInfoService;

    /**
     * 查询用户余额
     * @param userId
     * @return
     */
    @GetMapping("/queryBalance/{userId}")
    public Result queryBalance(@PathVariable("userId") Integer userId) {
        LambdaQueryWrapper<Wallet> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Wallet::getUserId, userId);
        Wallet wallet = WalletService.getOne(queryWrapper);
        if (wallet != null) {
            BigDecimal balance = wallet.getBalance();
            return Result.ok(balance);
        }
        return Result.fail("用户未开通钱包！！");
    }

    /**
     * 用户消费
     * @param userId
     * @param money
     * @return
     */
    @PostMapping("/payMoney")
    public Result payMoney(@RequestParam("userId") Integer userId,@RequestParam("money") BigDecimal money) {
        return WalletService.userPayMoney(userId,money);
    }
    /**
     * 用户收入
     * @param userId
     * @param money
     * @return
     */
    @PostMapping("/incomeMoney")
    public Result incomeMoney(@RequestParam("userId") Integer userId,@RequestParam("money") BigDecimal money) {
        return WalletService.userIncomeMoney(userId,money);
    }

    /**
     * 查询用户钱包金额变动明细
     * @param userId
     * @return
     */
    @GetMapping("/queryTradeInfo/{userId}")
    public Result queryTradeInfo(@PathVariable("userId") Integer userId) {
        LambdaQueryWrapper<TradeInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TradeInfo::getUserId, userId);
        TradeInfo tradeInfo = tradeInfoService.getOne(queryWrapper);
        return Result.ok(tradeInfo);
    }

}

